package com.sl.biz.modular.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sl.auth.ext.AuthExtension;
import com.sl.biz.modular.product.dao.entity.ProductDO;
import com.sl.biz.modular.product.dao.service.ProductDaoService;
import com.sl.biz.modular.product.service.ProductBizService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@RequiredArgsConstructor
public class ProductBizServiceImpl implements ProductBizService, AuthExtension {
    private final ProductDaoService productDaoService;

    @Override
    public Boolean save(ProductDO addRequest) {

        String id = addRequest.getId();
        if (StrUtil.isBlank(id)) {
            productDaoService.save(addRequest);
        } else {
            productDaoService.updateById(addRequest);
        }
        return Boolean.TRUE;
    }

    @Override
    public List<ProductDO> list() {
        List<ProductDO> processDataSourceDTOS = productDaoService.queryByList();
        return processDataSourceDTOS;
    }

    @Override
    public Boolean deleteById(ProductDO addRequest) {
        return productDaoService.removeById(addRequest.getId());
    }

    @Override
    public ProductDO queryByCode(String productCode) {
        ProductDO productDO = productDaoService.queryByCode(productCode);
        return productDO;
    }

    @InterceptorIgnore(tenantLine = "true")
    @Override
    public String getOrgIdByProductCode(String productCode) {
        LambdaQueryWrapper<ProductDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ProductDO::getProductCode, productCode);
        List<ProductDO> list = productDaoService.list(wrapper);
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        return CollUtil.get(list, 0).getOrgId();
    }
}
